import wx
import os

from utils import tracer
from wx.aui import AuiPaneInfo
from gui.mdiFrame import MDIFrame
from uiManager import UIManager

class ToolbarManager(UIManager):
  def __init__(self, name):
    UIManager.__init__(self, name)
    self._viewMenus = {}

  def load(self, pluginMgr):
    if pluginMgr is None:
      return

    tracer.writeln('Loading toolbars from plugins ...')

    self._items = {}

    plugins = pluginMgr.getPlugins()
    for plugin in plugins:
      mod = plugin.getModule()
      if mod is None:
        continue
      
      toolbar = mod.getToolbar()
      if not toolbar is None:
        self.add(toolbar.getName(), toolbar)
    
    tracer.writeln('Toolbars loaded. Count: %d%s' % (len(self._items), os.linesep))

  def attach(self, uiParent):
    if uiParent is None or self._items is None:
      return

    assert type(uiParent) is MDIFrame
    self._uiParent = uiParent

    tracer.writeln('%s attaching ...' % self.getName())

    auiMgr = self._uiParent.getAuiManager()
    
    tracer.writeln('Attaching toolbars of plugins ...')
    
    # attach toolbars to ui
    for name, toolbar in self._items.items():
      auiPaneInfo = self.createAuiPaneInfo(name, toolbar.getCaption())
      auiMgr.AddPane(toolbar, auiPaneInfo)
      
    auiMgr.Update()

    uiParent.auiPaneCloseEventHandler(self.onPaneClose)

    tracer.writeln(' Done%s' % os.linesep)

  def detach(self):
    if self._uiParent is None or self._items is None:
      return

    tracer.writeln('%s detaching ...' % self.getName())
    tracer.writeln('Detaching toolbars of plugins ...')

    auiMgr = self._uiParent.getAuiManager()
    for toolbar in self._items.values():
      auiMgr.DetachPane(toolbar)

    self._uiParent = None

    tracer.writeln(' Done%s' % os.linesep)

  def createMenus(self, uiParent):
    if uiParent is None:
      return None

    toolbars = self.getItems()
    if not toolbars is None:
      menus = []

      # create file menu
      view_menu = wx.Menu()

      view_menu_toolbars_menu = wx.Menu()

      for toolbar in toolbars:
        if toolbar is None:
          continue
        view_menu_toolbars_menu_sub = view_menu_toolbars_menu.AppendCheckItem(-1, toolbar.getCaption())
        view_menu_toolbars_menu_sub.Check(toolbar.IsShown())
        self._viewMenus[view_menu_toolbars_menu_sub.GetId()] = toolbar
        uiParent.Bind(wx.EVT_MENU, self.onToolbarMenuClicked, view_menu_toolbars_menu_sub)

      view_menu.AppendMenu(-1, 'Toolbars', view_menu_toolbars_menu)

      menus.append(('&View', view_menu))

      return menus

    return None

  def createAuiPaneInfo(self, name, caption):
    return AuiPaneInfo().Name(name).CaptionVisible(False).MaximizeButton(False).CloseButton(False).ToolbarPane().Top().Row(1).LeftDockable(False).RightDockable(False).BottomDockable(True)

  def add(self, name, toolbar):
    if self._items.has_key(name):
      return False
    self._items[name] = toolbar

  def remove(self, name):
    if self._items.has_key(name):
      del self._items[name]

  def clear(self):
    pass

  def onPaneClose(self, sender, args):
    caption = args.getValue()[0].GetPane().caption

    menuItem = self.getMenuItemByCaption(caption)
    if menuItem is None:
      return

    if menuItem.GetKind() == wx.ITEM_CHECK and menuItem.IsChecked():
      menuItem.Check(False)      

  def onToolbarMenuClicked(self, evt):
    if self._uiParent is None:
      evt.Skip()

    menuId = evt.GetId()
    if self._viewMenus.has_key(menuId):
      toolbar = self._viewMenus[menuId]
      auiMgr = self._uiParent.getAuiManager()
      if auiMgr is None:
        evt.Skip()
      
      auiInfo = auiMgr.GetPane(toolbar)
      auiInfo.Show(not auiInfo.IsShown())
      auiMgr.Update()
